<?
$vars['misc.odhelper.dir.lib'] = __DIR__;

function is_branch_dir($path = '.'){
    return is_dir(makePath($path, '.svn')) && is_dir(makePath($path, 'components'));
}
function adopt_to_branch_dir(&$dir){
    if(!is_branch_dir($dir)){
        if(is_dir($dir)) chdir($dir);
    }
}
function proc_prop($line, &$v, $vb = false){
    global $not_set, $show_notset;

    if($line === null) $line = $v;
    
    if(is_array($line)){
        foreach($line as $k => $l) $line[$k] = proc_prop($l, $v, $vb);
        return $line;
    }
    while(preg_match_all('/\$\{([\w\._\-]+)\}/', $line, $match)){
        for($i = sizeof($match[0]);--$i>=0;){
            $srch[] = '/'.preg_quote($match[0][$i], '/').'/';
            if($show_notset && !array_key_exists($match[1][$i], $v)) {
                $not_set[$match[0][$i]] = 1;
                if($vb) echo "!prop {$match[0][$i]} is not set\n";
            }
            $rplc[] = $v[$match[1][$i]];
        }
        $line = preg_replace($srch, $rplc, $line);
    }
    return $line;
}
function load_props($file, &$v){
    if(is_array($file)){
        foreach($file as $f)
            load_props($f, $v);
        return;
    } else {
        echo "loading properties($file)";
        if(!file_exists($file)){
            echo " ... ignoring(not exists)\n";
            return;
        } else echo "\n";
        $in = file($file);
        if($in === false) return false;
        foreach($in as $line){
            if($line[0]!='#'){
                list($key, $val) = explode('=', $line, 2);
                $key = preg_replace('/[\s\n]/', '', $key);
                if($key){
                    $v[$key] = preg_replace('/\n$/', '', $val);
                }
            }
        }
    }
}
function read_props(&$vars, $path = '.', $ignore_branch_props = false){
    if(defined('__PROPERTIES_PROCESSED')) return true; else define('__PROPERTIES_PROCESSED', true);
    global $config;

    foreach($config['defprops'] as $dp)
        load_props(makePath($path, procMeta($dp)), $vars);
        
    foreach($config['localprops'] as $dp)
        load_props(procMeta($dp), $vars);

    if(!$ignore_branch_props) load_props(makePath($path, $config['branch-props']), $vars);

    foreach($vars as $key => $val) $vars[$key] = proc_prop($val, $vars);
}
function log2file($file, $cont){
    $h = fopen($file, 'w+');
    fwrite($h, $cont);
    fclose($h);
}
function proc_prop_tpl($src, $dst, &$vars, $vb = true){
    if($vb) echo "processing tpl: $src -> $dst\n";
    $in = file($src);
    $in = join('', proc_prop($in, $vars, $vb));
    file_put_contents($dst, $in);
}
function label($str){
    echo str_pad($str, 40);
}

function makePath(){
    $arg = func_get_args();
    while(count($arg) && !$arg[0]) array_shift($arg);
    if(count($arg) > 1 && $arg[0] == '.' && substr($arg[1], 0, 2) == '/') array_shift($arg);
    $str = join('/', $arg);
    return preg_replace('/\/{2,}/', '/', $str);
}
function getRelativePath($path, $compareTo) {
    // clean arguments by removing trailing and prefixing slashes
    //echo "getRelativePath($path, $compareTo)\n";
    
    if ( substr( $path, -1 ) == '/' ) {
        $path = substr( $path, 0, -1 );
    }
    if ( substr( $path, 0, 1 ) == '/' ) {
        $path = substr( $path, 1 );
    }

    if ( substr( $compareTo, -1 ) == '/' ) {
        $compareTo = substr( $compareTo, 0, -1 );
    }
    if ( substr( $compareTo, 0, 1 ) == '/' ) {
        $compareTo = substr( $compareTo, 1 );
    }

    // simple case: $compareTo is in $path
    if ( strpos( $path, $compareTo ) === 0 ) {
        $offset = strlen( $compareTo ) + 1;
        return substr( $path, $offset );
    }
//echo "path: $path\ncompare: $compareTo\n========\n";
    $relative  = array(  );
    $pathParts = explode( '/', $path );
    $compareToParts = explode( '/', $compareTo );
    $flag = false;

    foreach( $compareToParts as $index => $part ) {
        if ( isset( $pathParts[$index] ) && $pathParts[$index] == $part ) {
            continue;
        }
        $relative[] = '..';
    }
    
    foreach( $pathParts as $index => $part ) {
        if ( isset( $compareToParts[$index] ) && $compareToParts[$index] == $part ) {
            if(!$flag) continue;
        }

        $relative[] = $part;
        $flag = true;
    }

    return implode( '/', $relative );
}
function rline($prompt="") {
    echo $prompt;
    $o = "";
    $c = "";
    while ($c!="\r"&&$c!="\n") {
        $o.= $c;
        $c = fread(STDIN, 1);
    }
//    fgetc(STDIN);
    return $o;
}
function procMeta($line){
    if(gettype($line) == 'array'){
        foreach($line as $i => $l)
            $line[$i] = procMeta($l);
        return $line;
    }

    if(preg_match_all('/\{\$([\w_]+)\}/', $line, $match)){
        $rplc = array();
        $srch = array();
        for($i = sizeof($match[1]); --$i >= 0;){
            if(defined($match[1][$i])){
                array_push($srch, '/' . preg_quote($match[0][$i], '/') . '/');
                array_push($rplc, constant($match[1][$i]));
            }
        }
        if(sizeof($rplc)) $line = preg_replace($srch, $rplc, $line);
    }
    return $line;
}

function curRC($fmt = true){
    if(date('N') > 3){
        $d = mktime(0, 0, 0, date('n'), (date('d') - date('N') + 3) + 7, date('Y'));
    } else {
        $d = mktime(0, 0, 0, date('n'), date('d') - date('N') + 3, date('Y'));
    }
    return $fmt 
        ? date('Ymd', $d)
        : $d;
}
function prevRC(){
    $d = curRC(false);
    return date('Ymd', mktime(0, 0, 0, date('n', $d), date('d', $d) - 7, date('Y', $d)));
}
function nextRC(){
    $d = curRC(false);
    return date('Ymd', mktime(0, 0, 0, date('n', $d), date('d', $d) + 7, date('Y', $d)));
}